package com.zhangcl.Threadtest;

public class TraditionarySynTest {
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		new TraditionarySynTest().init();
	}
	
	public void init(){
		Printer printer = new Printer();
		new Thread(new RunnableEx(printer, "abcdefghijklmnopqrst")).start();
		new Thread(new RunnableEx(printer, "12345678901234567890")).start();
	}
	
	class RunnableEx implements Runnable {
		
		String printStr;
		Printer printer;
		public RunnableEx(Printer printer, String str){
			this.printer = printer;
			this.printStr = str;
		}
		
		@Override
		public void run() {
			while (true) {
				try {
					Thread.sleep(10);
					this.printer.print3(this.printStr);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		}
		
	}
	
	class Printer{
		public synchronized void print1(String str){
			int len = str.length();
			for (int i = 0; i < len; i++) {
				System.out.print(str.charAt(i));
			}
			System.out.println();
		}
		
		public void print2(String str){
			synchronized (this) {
				int len = str.length();
				for (int i = 0; i < len; i++) {
					System.out.print(str.charAt(i));
				}
				System.out.println();
			}
		}
		
		public void print3(String str){
			synchronized (Printer.class) {
				int len = str.length();
				for (int i = 0; i < len; i++) {
					System.out.print(str.charAt(i));
				}
				System.out.println();
			}
		}
	}
}
